package PLout;

use BioProof;
use strict;

sub new {
	my ($class,$job) = @_;

	my $self = {};
	$self->{job} = $job;
	$self->{verify} = "";
	$self->{codons} = "";
	$self->{primers} = "";

	my $jobname = "Results for job ";
	
	if($job->{title} eq ""){
		$jobname .= $job->GetID;
	} else {
		$jobname .= "'" . $job->{title} . "'";
	}
	
	$self->{title} = $jobname;

	bless $self, $class;
	return $self;		
}

sub SetVerify {
	my ($self,$out) = @_;
	$self->{verify} = $out;
}

sub SetPrimers {
	my ($self,$out) = @_;
	$self->{primers} = $out;
}

sub SetCodons {
	my ($self,$out) = @_;
	$self->{codons} = $out;
}

sub GetName {
	my $self = shift;
	return $self->{title};
}

sub OutputCodons {
	my ($self, $html) =@_;

	return "" if($self->{codons} eq "");

	my @o = split /\n/ , $self->{codons};
	my $bad = $o[0];
	$bad =~ s/^\s//g;
	$bad =~ s/\s$//g;
	$bad =~ s/,/\n/g;
	
	my $out = "<h2>Coding regions</h2>";
	if($html == 0){
		$out =~ s/<\/?h2>//g;
		$out .= "\n";
	}
	
	my $ratio = $o[1];
	my ($numgood, $total) = (0,0);
	if($ratio =~ m/Ratio: (\d+)\/(\d+)/){
		$numgood = $1;
		$total = $2;
	}


	if($bad eq ""){
		$out .= "All regions are coding\n";
	} else {
		my $in =  $total - $numgood . " out of $total sequences are not coding:";
		$out .= "<b>$in</b><br>" if($html == 1);
		$out .= "$in\n\n" if($html == 0);
		$out .= $bad . "\n";
	}

	$out =~ s/\n/<br>\n/g if($html == 1);

	return $out;
}

sub Output {
	my ($self,$html) = @_;
	my $job = $self->{job};
	my $id = $job->{id};
	my $jobname = $job->{title};

	my $beg =  "<html>\n<head>\n<title>Job results for job $jobname</title>\n</head>\n<body>\n";
	my $info = "<h1>Results</h1><hr>\n";
	my $end = "</body>\n</html>";
	my $v = $self->{verify};
	my $lines = "----------------------------------\n";
	
	my $vi = "";

	my @veri = ("Possibly contaminated sequences:","The following did not retrieve any results:","No sequences appear to be contaminated!");

	$vi = "\n<h2>Verification of sequences</h2>" if($v ne "");
	if($html == 1){
		$v =~ s/\n/\n<br>/g;
		$v =~ s/\t/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/g;
		foreach(@veri){
			$v =~ s/$_/<i>$_<\/i>/;
		}
	} else {
		$beg = "";
		$end = "";
		$vi =~ s/<\/?h2>//g;
		$vi = $lines . $vi . "\n";
		$v .= "\n" . $lines;
		$info =~ s/<\/?[^>]+>//g;
		$info .= "\n";
	}

	$v = $vi . $v;

	my $cod = OutputCodons($self,$html);

	my $out = $beg . $info . $v . $cod . $end;

	return $out;
}


sub OutputHTML {
	my $self = shift;
	return Output($self,1);
}

sub OutputText {
	my $self = shift;
	return Output($self,0);
}
1;
